﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cotrobaie.UsersWebService;
namespace Cotrobaie
{
    public partial class MyAccount : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ButtonStatistics.Attributes.Add("onMouseOut", "this.src='/images/comm.png'");
            ButtonStatistics.Attributes.Add("onMouseOver", "this.src='/images/ocomm.png'");
            ButtonDescr.Attributes.Add("onMouseOut", "this.src='/images/recc.png'");
            ButtonDescr.Attributes.Add("onMouseOver", "this.src='/images/orecc.png'");
            ButtonComments.Attributes.Add("onMouseOut", "this.src='/images/acc.png'");
            ButtonComments.Attributes.Add("onMouseOver", "this.src='/images/oacc.png'");

            if (Session["UserID"] == null) Response.Redirect("Home.aspx");
            if (!Page.IsPostBack)
            {
                LabelUserName.Text = Session["UserUsername"].ToString();
                TextBoxNume.Text = Session["UserNume"].ToString();
                TextBoxPrenume.Text = Session["UserPrenume"].ToString();
                TextBoxEmail.Text = Session["UserEmail"].ToString();
            }
            else if (Session["UserNewParola"] != null)
            { Session["UserParola"] = Session["UserNewParola"]; Session["UserNewParola"] = null; }
        }

        protected void UsernameBone_TextChanged(object sender, EventArgs e)
        {
            if (Session["UserEmail"].ToString() != TextBoxEmail.Text)
            {
                CuVEmailBona.IsValid = NonPublic.GetUserByEmail(TextBoxEmail.Text) == null;
                if (!CuVEmailBona.IsValid) {ErrorMessage.Text = "Adresa de E-mail exista deja in baza noastra de date. <br>Daca ati uitat numele de utilizator sau parola <br>puteti accesa pagina de <a href='RecupereazaParola.aspx'>recuperare a parolei</a>";return;}
            }
        }

        protected void ButtonChangeAccountInfo_Click(object sender, EventArgs e)
        {
            using (UsersSoapClient serviciu = new UsersSoapClient("UsersSoap"))
            {
                var result = serviciu.SchimbaProfil((int)Session["UserID"], Session["UserUsername"].ToString(), Session["UserParola"].ToString(), TextBoxNume.Text, TextBoxPrenume.Text, TextBoxEmail.Text, TextBoxParola.Text);
                if (!result.Success) { ErrorMessage.Text = result.Mesaj; return; }
                else
                {
                    Session["UserNume"] = TextBoxNume.Text;
                    Session["UserPrenume"] = TextBoxPrenume.Text;
                    Session["UserNewParola"] = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(TextBoxParola.Text, "MD5");
                    Session["UserEmail"] = TextBoxEmail.Text;
                    ErrorMessage.Text = result.Mesaj;
                }
            }
            Page_Load(sender, e);
        }

        public void tabClick(object sender, EventArgs e)
        {
            ImageButton but = (ImageButton)sender;

            if (but.ID == "ButtonDescr")
            {
                Response.Redirect("~/Recommendations.aspx");
            }

            else if (but.ID == "ButtonComments")
            {
                Response.Redirect("~/MyAccount.aspx");
            }

            else if (but.ID == "ButtonStatistics")
            {
                Response.Redirect("~/Comments.aspx");
            }

        }

        protected void goHome(object sender, EventArgs e)
        {
            Response.Redirect("~/Home.aspx");
        }
        
    }
}